package com.ibiz.znjd;

import android.app.Service;
import android.os.Vibrator;

import com.baidu.location.LocationClient;
import com.baidu.mapapi.CoordType;
import com.baidu.mapapi.SDKInitializer;
import com.ibiz.znjd.baidu.face.ExampleApplication;
import com.ibiz.znjd.baidu.face.exception.FaceException;
import com.ibiz.znjd.baidu.face.manager.APIService;
import com.ibiz.znjd.baidu.face.model.AccessToken;
import com.ibiz.znjd.baidu.face.utils.OnResultListener;
import com.ibiz.znjd.baidu.location.GeoFenceService;
import com.ibiz.znjd.baidu.location.LocationService;
import com.ibiz.znjd.utils.ForegroundService;

import java.util.HashMap;
import java.util.Map;

public class MainApplication extends ExampleApplication {
    /**
     * 全局唯一的定位服务。
     */
    private LocationService locationService;

    /**
     * 围栏服务
     */
    public GeoFenceService geoFenceService;

    /**
     * 前台服务
     */
    public ForegroundService foregroundService;

    /**
     * 上次定时传轨迹的时间戳
     */
    public long lastRecordTime = -1;

    /**
     * 全局环境参数
     */
    public Map<String,String> envMap = new HashMap<>();

    /**
     * 全局唯一的振动器
     */
    public Vibrator mVibrator;

    /**
     * 初始化方法
     */
    @Override
    public void onCreate() {
        super.onCreate();
        // 初始化振动器
        mVibrator =(Vibrator)getApplicationContext().getSystemService(Service.VIBRATOR_SERVICE);
        // 百度地图sdk初始化
        SDKInitializer.setAgreePrivacy(getApplicationContext(), true);
        SDKInitializer.initialize(getApplicationContext());
        SDKInitializer.setCoordType(CoordType.BD09LL);
        // 百度人脸识别初始化，获取accesstoken
        APIService.getInstance().init();
        APIService.getInstance().initAccessTokenWithAkSk(new OnResultListener<AccessToken>() {
            @Override
            public void onResult(AccessToken result) {
            }

            @Override
            public void onError(FaceException error) {
            }
        },"tDrFXZdpqj98RcF1sy5VSWHG","oOyj91oRLYjFge0UO69XG71E2n24KyRM");
    }

    /**
     * 获取locationService,被调用后才会全局生成一个唯一的LocationService服务。
     * @return
     */
    public LocationService getLocationService(){
        if(locationService == null){
            // 百度sdk自v9.2.9起，需要获取先同意隐私政策。
            LocationClient.setAgreePrivacy(true);
            LocationService.getPermissions(getApplicationContext());
            locationService = new LocationService(getApplicationContext());
        }
        return locationService;
    }

}
